package com.guigu.youxuan.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.pojo.BackUser;
import com.guigu.youxuan.pojo.Warehouse;
import com.guigu.youxuan.service.IWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/warehouse")
public class WarehouseController {
    /**
     * 导入仓库接口
     */
    @Autowired
    private IWarehouseService iWarehouseService;

    /**
     * 查询仓库列表
     * @return
     */
    @RequestMapping("/queryWarehouse")
    @ResponseBody
    public List<Warehouse> queryWarehouse() {
        List<Warehouse> list = iWarehouseService.queryWarehouse();
        return list;
    }


    /**
     * 仓库管理分页查询仓库数据
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/warehouseListPage/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<Warehouse> WarehouseListPage(@PathVariable int currentPage,
                                                  @PathVariable int pageSize
    ){
        PageInfo<Warehouse> page=this.iWarehouseService.WarehouseListPage(currentPage,pageSize);
        return page;
    }

    /**
     * 添加仓库
     * @param warehouse
     * @return
     */
    @RequestMapping("/storageAdd")
    @ResponseBody
    public String StorageAddform(@RequestBody Warehouse warehouse){
        //设置标志位属性
        //仓库状态
        warehouse.setCheckTag("S001-1");
        //登记时间
        warehouse.setRegisterTime(new Date());
        //添加
        boolean flag =this.iWarehouseService.save(warehouse);
        if (flag){
            return "ok";
        }
            return "error";
    }

    /**
     * 根据仓库表id查询对应的仓库信息
     * @param id
     * @return
     */
    @RequestMapping("/updwarehouseById/{id}")
    @ResponseBody
    public Warehouse updwarehouseById(@PathVariable int id){
        Warehouse warehouse=this.iWarehouseService.getByListId(id);
        return warehouse;
    }

    /**
     * 对仓库列表信息修改
     * @param warehouse
     * @return
     */
    @RequestMapping("/updatewarehouseById")
    @ResponseBody
    public String updatewarehouseById(@RequestBody Warehouse warehouse){
        boolean flag=this.iWarehouseService.updateById(warehouse);
        if (flag){
            return "ok";
        }
        return "error";
    }

    /**
     * 仓库审核查询未审核的仓库信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/WarehouseFuhePage/{currentPage}/{pageSize}")
    @ResponseBody
    public PageInfo<Warehouse> WarehouseFuhePage(@PathVariable int currentPage,
                                                 @PathVariable int pageSize
    ){
        PageInfo<Warehouse> page=this.iWarehouseService.WarehouseFuhePage(currentPage,pageSize);
        return page;
    }

    /**
     * 仓库审核信息复核通过
     * @param warehouse
     * @return
     */
    @RequestMapping("/fuhewarehouseById")
    @ResponseBody
    public String fuhewarehouseById(@RequestBody Warehouse warehouse) {
        warehouse.setCheckTag("S001-3");
        warehouse.setCheckTime(new Date());
        System.out.println("审核"+warehouse.getCheckTag()+"审核");
        System.out.println("id"+warehouse.getWarehouseId()+"id");
        boolean flag=this.iWarehouseService.updateById(warehouse);
        if(flag){
            return "ok";
        }
        return "error";
    }
    /**
     * 仓库审核信息复核不通过
     * @param warehouse
     * @return
     */
    @RequestMapping("/fuhenowarehouseById")
    @ResponseBody
    public String fuhenowarehouseById(@RequestBody Warehouse warehouse) {
        warehouse.setCheckTag("S001-2");
        warehouse.setCheckTime(new Date());
        System.out.println("审核"+warehouse.getCheckTag()+"审核");
        System.out.println("id"+warehouse.getWarehouseId()+"id");
        boolean flag=this.iWarehouseService.updateById(warehouse);
        if(flag){
            return "ok";
        }
        return "error";
    }
    /**
     * 仓库禁用/启用
     * @param warehouse
     * @return
     */
    @RequestMapping("/switchStatus")
    public String switchStatus(@RequestBody Warehouse warehouse) {
        iWarehouseService.switchStatus(warehouse);
        if (warehouse.getStatus() == 0) {
            return "启用成功";
        }
        return "禁用成功";
    }


}
